package main

import (
	"context"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
	"turingApi/conf"
	"turingApi/model"
	"turingApi/router"
)

func init() {
}

func main() {

	//初始化配置
	conf.Init()

	// 初始化数据库
	model.Init()
	defer model.Close()
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
	conf.Logger.Info("app is running...")

	app := router.Init()
	go func() {
		if err := app.Start(conf.App.Addr); err != nil {
			conf.Logger.Fatal(err.Error())
		}
	}()
	<-quit
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := app.Shutdown(ctx); err != nil {
		if err != http.ErrServerClosed { //Normal exit
			conf.Logger.Error("Server Shutdown:", err)
		}
	}
	conf.Logger.Info("app has been quited!")

}
